import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import path from 'node:path';
import { viteMockServe } from 'vite-plugin-mock';
import svgLoader from 'vite-svg-loader';

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd());
  return {
    base: env.VITE_BASE_URL,
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
      },
    },
    css: {
      preprocessorOptions: {
        less: {
          modifyVars: {
            hack: `true; @import (reference) "${path.resolve('src/style/variables.less')}";`,
          },
          math: 'strict',
          javascriptEnabled: true,
        },
      },
    },
    plugins: [
      vue(),
      vueJsx(),
      viteMockServe({
        mockPath: 'mock',
        enable: mode === 'mock',
      }),
      svgLoader(),
    ],
    optimizeDeps: {
      include: [
        'bpmn-js',
        'bpmn-js/lib/Modeler',
        'tdesign-icons-vue-next',
      ],
      exclude: ['@vue/devtools-api'],
    },
    build: {
      chunkSizeWarningLimit: 2000,
      rollupOptions: {
        output: {
          manualChunks: {
            'bpmn-js': ['bpmn-js'],
            'tdesign-icons': ['tdesign-icons-vue-next'],
          },
        },
      },
    },
    server: {
      host: '0.0.0.0',
      port: 3002,
      cors: true,
      proxy: {
        [env.VITE_API_URL_PREFIX]: {
          target: env.VITE_API_URL,
          changeOrigin: true,
          rewrite: (path) => path.replace(new RegExp(`^${env.VITE_API_URL_PREFIX}`), ''),
        },
      },
    },
  };
});
